using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace shuju_jiange
{
    public partial class FrmSetStartPoint : Form
    {
        private Form1 parentForm;
        private List<double> allData;
        private int currentIndex;
        private int startPoint;
        private Dictionary<int, int> baseGroupData;
        private Timer runTimer;
        private Dictionary<int, Dictionary<int, int>> historicalGroupData; // 缓存历史数据点
        private Dictionary<int, Series> historicalSeries; // 缓存历史数据系列
        
        public FrmSetStartPoint(Form1 parent)
        {
            InitializeComponent();
            this.parentForm = parent;
            historicalGroupData = new Dictionary<int, Dictionary<int, int>>();
            historicalSeries = new Dictionary<int, Series>();
            InitializeChart();
        }
        
        private void InitializeChart()
        {
            chartResult.Series.Clear();
            chartResult.ChartAreas.Clear();
            
            ChartArea chartArea = new ChartArea();
            chartArea.Name = "MainArea";
            chartArea.AxisX.Title = "出现次数";
            chartArea.AxisY.Title = "次数的出现次数";
            chartArea.AxisX.MajorGrid.Enabled = true;
            chartArea.AxisY.MajorGrid.Enabled = true;
            
            /* 关键缩放代码 start */
            // Enable range selection and zooming end user interface for X axis
            chartArea.CursorX.IsUserEnabled = true;
            chartArea.CursorX.IsUserSelectionEnabled = true;
            chartArea.AxisX.ScaleView.Zoomable = true;
            
            // Enable range selection and zooming end user interface for Y axis
            chartArea.CursorY.IsUserEnabled = true;
            chartArea.CursorY.IsUserSelectionEnabled = true;
            chartArea.AxisY.ScaleView.Zoomable = true;
            
            // 将滚动内嵌到坐标轴中
            chartArea.AxisX.ScrollBar.IsPositionedInside = true;
            chartArea.AxisY.ScrollBar.IsPositionedInside = true;
            
            // 设置滚动条的大小
            chartArea.AxisX.ScrollBar.Size = 20;
            chartArea.AxisY.ScrollBar.Size = 20;
            
            // 设置滚动条的按钮的风格，下面代码是将所有滚动条上的按钮都显示出来
            chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
            chartArea.AxisY.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
            
            // 设置自动放大与缩小的最小量
            chartArea.AxisX.ScaleView.SmallScrollSize = double.NaN;
            chartArea.AxisX.ScaleView.SmallScrollMinSize = 2;
            chartArea.AxisY.ScaleView.SmallScrollSize = double.NaN;
            chartArea.AxisY.ScaleView.SmallScrollMinSize = 2;
            /* 关键缩放代码 end */
            
            chartResult.ChartAreas.Add(chartArea);
            
            // 基础数据系列（启动点前的数据，蓝色线+点）
            Series baseSeries = new Series();
            baseSeries.Name = "基础数据";
            baseSeries.ChartType = SeriesChartType.Line;
            baseSeries.MarkerStyle = MarkerStyle.Circle;
            baseSeries.MarkerSize = 8;
            baseSeries.BorderWidth = 2;
            baseSeries.Color = Color.Blue;
            baseSeries.MarkerColor = Color.Blue;
            baseSeries.MarkerBorderColor = Color.Blue;
            baseSeries.MarkerBorderWidth = 1;
            chartResult.Series.Add(baseSeries);
            
            // 新增数据系列（启动点后的数据，红色点，不连线）
            Series newDataSeries = new Series();
            newDataSeries.Name = "新增数据";
            newDataSeries.ChartType = SeriesChartType.Point;
            newDataSeries.MarkerStyle = MarkerStyle.Circle;
            newDataSeries.MarkerSize = 10;
            newDataSeries.Color = Color.Red;
            newDataSeries.MarkerColor = Color.Red;
            newDataSeries.MarkerBorderColor = Color.Red;
            newDataSeries.MarkerBorderWidth = 2;
            chartResult.Series.Add(newDataSeries);
            
            // 当前处理点系列（用于高亮显示）
            Series currentSeries = new Series();
            currentSeries.Name = "当前点";
            currentSeries.ChartType = SeriesChartType.Point;
            currentSeries.MarkerStyle = MarkerStyle.Circle;
            currentSeries.MarkerSize = 8;
            currentSeries.Color = Color.Orange;
            currentSeries.MarkerColor = Color.Orange;
            currentSeries.MarkerBorderColor = Color.DarkOrange;
            currentSeries.MarkerBorderWidth = 3;
            chartResult.Series.Add(currentSeries);
        }
        
        private void btnStartDraw_Click(object sender, EventArgs e)
        {
            try
            {
                // 获取启动点
                if (!int.TryParse(txtStartPoint.Text, out int startPoint) || startPoint <= 0)
                {
                    MessageBox.Show("请输入有效的启动点数值（大于0的整数）", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
                // 从Form1获取数据
                allData = GetDataFromParentForm();
                if (allData == null || allData.Count == 0)
                {
                    MessageBox.Show("未找到数据，请先在主窗体中导入数据", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
                // 检查启动点是否大于数据总数
                if (startPoint > allData.Count)
                {
                    MessageBox.Show($"启动点({startPoint})不能大于数据总数({allData.Count})", "参数错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
                // 清理缓存
                ClearCache();
                
                this.startPoint = startPoint;
                 currentIndex = startPoint;  // 从启动点开始显示基础数据
                
                // 初始化基础分组数据
                InitializeBaseGroupData();
                
                // 启用导航按钮
                btnNext.Enabled = currentIndex < allData.Count;
                btnPrevious.Enabled = false;
                btnRunToEnd.Enabled = currentIndex < allData.Count;
                btnCalculateOnly.Enabled = currentIndex < allData.Count;
                
                ProcessAndDrawChart();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"处理数据时发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        private void ClearCache()
        {
            historicalGroupData.Clear();
            historicalSeries.Clear();
        }
        
        private List<double> GetDataFromParentForm()
        {
            List<double> data = new List<double>();
            
            // 从Form1的richTextBox1获取数据
            if (parentForm?.richTextBox1?.Text != null)
            {
                string[] dataArray = parentForm.richTextBox1.Text.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                
                foreach (string item in dataArray)
                {
                    if (double.TryParse(item, out double value))
                    {
                        data.Add(value);
                    }
                }
            }
            
            return data;
        }
        
        private void InitializeBaseGroupData()
        {
            try
            {
                // 获取启动点前的数据（0到startPoint-1）
                var baseData = allData.Take(startPoint).ToList();

                // 第一步分组：按数据值分组，统计每个数据值的出现次数（M）
                var firstGrouping = baseData
                    .GroupBy(x => x)
                    .Select(g => new { Value = g.Key, Count = g.Count() })
                    .ToList();

                // 第二步分组：按出现次数分组，统计次数的出现次数（N）
                 baseGroupData = firstGrouping
                     .GroupBy(x => x.Count)
                     .ToDictionary(g => g.Key, g => g.Count());
            }
            catch (Exception ex)
            {
                MessageBox.Show($"初始化基础数据时发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                baseGroupData = new Dictionary<int, int>();
            }
        }
        
        private void ProcessAndDrawChart()
        {
            try
            {
                // 获取0到当前索引的数据（X）
                var currentData = allData.Take(currentIndex + 1).ToList();

                // 第一步分组：按数据值分组，统计每个数据值的出现次数（M）
                var firstGrouping = currentData
                    .GroupBy(x => x)
                    .Select(g => new { Value = g.Key, Count = g.Count() })
                    .ToList();

                // 第二步分组：按出现次数分组，统计次数的出现次数（N）
                var secondGrouping = firstGrouping
                    .GroupBy(x => x.Count)
                    .Select(g => new { Count = g.Key, Frequency = g.Count() })
                    .OrderBy(x => x.Count)
                    .ToList();

                // 清空图表
                 chartResult.Series.Clear();
                 chartResult.ChartAreas.Clear();

                 // 创建图表区域
                 var chartArea = new ChartArea("MainArea");
                 chartArea.AxisX.Title = "次数";
                 chartArea.AxisY.Title = "个数";
                 chartArea.AxisX.MajorGrid.Enabled = true;
                 chartArea.AxisY.MajorGrid.Enabled = true;
                 
                 /* 关键缩放代码 start */
                 // Enable range selection and zooming end user interface for X axis
                 chartArea.CursorX.IsUserEnabled = true;
                 chartArea.CursorX.IsUserSelectionEnabled = true;
                 chartArea.AxisX.ScaleView.Zoomable = true;
                 
                 // Enable range selection and zooming end user interface for Y axis
                 chartArea.CursorY.IsUserEnabled = true;
                 chartArea.CursorY.IsUserSelectionEnabled = true;
                 chartArea.AxisY.ScaleView.Zoomable = true;
                 
                 // 将滚动内嵌到坐标轴中
                 chartArea.AxisX.ScrollBar.IsPositionedInside = true;
                 chartArea.AxisY.ScrollBar.IsPositionedInside = true;
                 
                 // 设置滚动条的大小
                 chartArea.AxisX.ScrollBar.Size = 20;
                 chartArea.AxisY.ScrollBar.Size = 20;
                 
                 // 设置滚动条的按钮的风格，下面代码是将所有滚动条上的按钮都显示出来
                 chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
                 chartArea.AxisY.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
                 
                 // 设置自动放大与缩小的最小量
                 chartArea.AxisX.ScaleView.SmallScrollSize = double.NaN;
                 chartArea.AxisX.ScaleView.SmallScrollMinSize = 2;
                 chartArea.AxisY.ScaleView.SmallScrollSize = double.NaN;
                 chartArea.AxisY.ScaleView.SmallScrollMinSize = 2;
                 /* 关键缩放代码 end */
                 
                 chartResult.ChartAreas.Add(chartArea);

                // 如果当前索引等于启动点，绘制基础数据（蓝色线条连接）
                if (currentIndex == startPoint)
                {
                    var baseSeries = new Series("基础数据")
                    {
                        ChartType = SeriesChartType.Line,
                        Color = Color.Blue,
                        BorderWidth = 2
                    };

                    foreach (var item in secondGrouping.OrderBy(x => x.Count))
                    {
                        baseSeries.Points.AddXY(item.Count, item.Frequency);
                    }
                    chartResult.Series.Add(baseSeries);
                    
                    // 保存基础数据用于后续比较
                    baseGroupData = secondGrouping.ToDictionary(x => x.Count, x => x.Frequency);
                }
                else if (currentIndex > startPoint)
                {
                    // 绘制基础数据（蓝色线条）
                    if (baseGroupData != null && baseGroupData.Any())
                    {
                        var baseSeries = new Series("基础数据")
                        {
                            ChartType = SeriesChartType.Line,
                            Color = Color.Blue,
                            BorderWidth = 2
                        };

                        foreach (var item in baseGroupData.OrderBy(x => x.Key))
                        {
                            baseSeries.Points.AddXY(item.Key, item.Value);
                        }
                        chartResult.Series.Add(baseSeries);
                    }

                    // 优化：只绘制缓存的历史数据点（克隆系列以避免重复添加问题）
                    foreach (var kvp in historicalSeries)
                    {
                        if (kvp.Key < currentIndex) // 只添加历史数据点
                        {
                            var clonedSeries = new Series($"历史数据_{kvp.Key}")
                            {
                                ChartType = kvp.Value.ChartType,
                                Color = Color.Red, // 历史数据用红色
                                MarkerStyle = kvp.Value.MarkerStyle,
                                MarkerSize = 6
                            };
                            
                            foreach (var point in kvp.Value.Points)
                            {
                                clonedSeries.Points.AddXY(point.XValue, point.YValues[0]);
                            }
                            
                            chartResult.Series.Add(clonedSeries);
                        }
                    }
                    
                    // 计算当前数据的分组并缓存
                    var currentGroupData = secondGrouping.ToDictionary(x => x.Count, x => x.Frequency);
                    historicalGroupData[currentIndex] = currentGroupData;

                    // 绘制当前数据（绿色点，表示当前步骤）
                    var newSeries = new Series($"当前数据_{currentIndex}")
                    {
                        ChartType = SeriesChartType.Point,
                        Color = Color.Green,  // 当前数据用绿色
                        MarkerStyle = MarkerStyle.Circle,
                        MarkerSize = 18
                    };

                    // 只绘制发生变化的数据点
                    if (currentIndex > startPoint && historicalGroupData.ContainsKey(currentIndex - 1))
                    {
                        var previousGroupData = historicalGroupData[currentIndex - 1];
                        
                        // 找出发生变化的数据点
                        foreach (var item in secondGrouping)
                        {
                            int previousFrequency = previousGroupData.ContainsKey(item.Count) ? previousGroupData[item.Count] : 0;
                            if (item.Frequency != previousFrequency)
                            {
                                newSeries.Points.AddXY(item.Count, item.Frequency);
                            }
                        }
                        
                        // 检查是否有在当前步骤中消失的数据点（频率变为0）
                        foreach (var prevItem in previousGroupData)
                        {
                            if (!currentGroupData.ContainsKey(prevItem.Key))
                            {
                                newSeries.Points.AddXY(prevItem.Key, 0);
                            }
                        }
                    }
                    else
                    {
                        // 第一步时绘制所有数据点
                        foreach (var item in secondGrouping.OrderBy(x => x.Count))
                        {
                            newSeries.Points.AddXY(item.Count, item.Frequency);
                        }
                    }
                    
                    // 缓存当前系列
                    historicalSeries[currentIndex] = newSeries;
                    chartResult.Series.Add(newSeries);
                }

                // 更新图表标题
                chartResult.Titles.Clear();
                chartResult.Titles.Add($"次数分布图 - 当前位置: {currentIndex}, 数据值: {(currentIndex < allData.Count ? allData[currentIndex].ToString() : "完成")}");

                // 更新进度标签
                lblProgress.Text = $"进度: {currentIndex + 1}/{allData.Count}";

                // 刷新图表
                chartResult.Invalidate();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"绘制图表时发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // 更新按钮状态
            btnPrevious.Enabled = currentIndex > startPoint;
            btnNext.Enabled = currentIndex < allData.Count;
            btnRunToEnd.Enabled = currentIndex < allData.Count;
        }
        
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentIndex < allData.Count)
            {
                currentIndex++;
                ProcessAndDrawChart();
            }
        }
        
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currentIndex > startPoint)
            {
                currentIndex--;
                ProcessAndDrawChart();
            }
        }
        
        private void btnRunToEnd_Click(object sender, EventArgs e)
        {
            // 禁用所有按钮，防止在运行过程中被点击
            btnNext.Enabled = false;
            btnPrevious.Enabled = false;
            btnRunToEnd.Enabled = false;
            btnCalculateOnly.Enabled = false;
            btnStartDraw.Enabled = false;
            
            // 使用Timer来实现逐步运行，避免界面卡死
            var timer = new System.Windows.Forms.Timer();
            timer.Interval = 100; // 每100毫秒执行一次
            timer.Tick += (s, args) =>
            {
                if (currentIndex < allData.Count)
                {
                    currentIndex++;
                    ProcessAndDrawChart();
                    
                    // 强制刷新界面
                    Application.DoEvents();
                }
                else
                {
                    // 运行完成，停止定时器并恢复按钮状态
                    timer.Stop();
                    timer.Dispose();
                    
                    btnPrevious.Enabled = currentIndex > startPoint;
                    btnNext.Enabled = currentIndex < allData.Count;
                    btnRunToEnd.Enabled = currentIndex < allData.Count;
                    btnCalculateOnly.Enabled = currentIndex < allData.Count;
                    btnStartDraw.Enabled = true;
                }
            };
            timer.Start();
        }
        
        private void btnCalculateOnly_Click(object sender, EventArgs e)
        {
            try
            {
                // 禁用所有按钮，防止在计算过程中被点击
                btnNext.Enabled = false;
                btnPrevious.Enabled = false;
                btnRunToEnd.Enabled = false;
                btnCalculateOnly.Enabled = false;
                btnStartDraw.Enabled = false;
                
                // 显示计算中的提示
                lblProgress.Text = "正在计算中...";
                Application.DoEvents(); // 强制刷新界面显示提示信息
                
                // 保存当前索引，以便计算完成后恢复
                int originalIndex = currentIndex;
                
                // 直接计算到最后一个数据点，但不绘制图表
                currentIndex = allData.Count - 1;
                
                // 计算所有数据点的分组结果
                var currentData = allData.Take(currentIndex + 1).ToList();
                
                // 第一步分组：按数据值分组，统计每个数据值的出现次数（M）
                var firstGrouping = currentData
                    .GroupBy(x => x)
                    .Select(g => new { Value = g.Key, Count = g.Count() })
                    .ToList();
                
                // 第二步分组：按出现次数分组，统计次数的出现次数（N）
                var secondGrouping = firstGrouping
                    .GroupBy(x => x.Count)
                    .Select(g => new { Count = g.Key, Frequency = g.Count() })
                    .OrderBy(x => x.Count)
                    .ToList();
                
                // 计算完成后，绘制图表
                lblProgress.Text = "正在绘制图表...";
                Application.DoEvents(); // 强制刷新界面显示提示信息
                
                // 清空图表
                chartResult.Series.Clear();
                chartResult.ChartAreas.Clear();
                
                // 创建图表区域
                var chartArea = new ChartArea("MainArea");
                chartArea.AxisX.Title = "次数";
                chartArea.AxisY.Title = "个数";
                chartArea.AxisX.MajorGrid.Enabled = true;
                chartArea.AxisY.MajorGrid.Enabled = true;
                
                /* 关键缩放代码 start */
                 // Enable range selection and zooming end user interface for X axis
                 chartArea.CursorX.IsUserEnabled = true;
                 chartArea.CursorX.IsUserSelectionEnabled = true;
                 chartArea.AxisX.ScaleView.Zoomable = true;
                 
                 // Enable range selection and zooming end user interface for Y axis
                 chartArea.CursorY.IsUserEnabled = true;
                 chartArea.CursorY.IsUserSelectionEnabled = true;
                 chartArea.AxisY.ScaleView.Zoomable = true;
                 
                 // 将滚动内嵌到坐标轴中
                 chartArea.AxisX.ScrollBar.IsPositionedInside = true;
                 chartArea.AxisY.ScrollBar.IsPositionedInside = true;
                 
                 // 设置滚动条的大小
                 chartArea.AxisX.ScrollBar.Size = 20;
                 chartArea.AxisY.ScrollBar.Size = 20;
                 
                 // 设置滚动条的按钮的风格，下面代码是将所有滚动条上的按钮都显示出来
                 chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
                 chartArea.AxisY.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
                 
                 // 设置自动放大与缩小的最小量
                 chartArea.AxisX.ScaleView.SmallScrollSize = double.NaN;
                 chartArea.AxisX.ScaleView.SmallScrollMinSize = 2;
                 chartArea.AxisY.ScaleView.SmallScrollSize = double.NaN;
                 chartArea.AxisY.ScaleView.SmallScrollMinSize = 2;
                 /* 关键缩放代码 end */
                
                chartResult.ChartAreas.Add(chartArea);
                
                // 绘制基础数据（蓝色线条）
                if (baseGroupData != null && baseGroupData.Any())
                {
                    var baseSeries = new Series("基础数据")
                    {
                        ChartType = SeriesChartType.Line,
                        Color = Color.Blue,
                        BorderWidth = 2
                    };
                    
                    foreach (var item in baseGroupData.OrderBy(x => x.Key))
                    {
                        baseSeries.Points.AddXY(item.Key, item.Value);
                    }
                    chartResult.Series.Add(baseSeries);
                }
                
                // 优化：使用缓存的历史数据点，避免重复计算
                if (historicalSeries.Any())
                {
                    // 绘制缓存的历史数据点
                    foreach (var kvp in historicalSeries.OrderBy(x => x.Key))
                    {
                        bool isLastPoint = (kvp.Key == currentIndex);
                        var clonedSeries = new Series(isLastPoint ? "最终数据" : $"历史数据_{kvp.Key}")
                        {
                            ChartType = SeriesChartType.Point,
                            Color = isLastPoint ? Color.Green : Color.Red,
                            MarkerStyle = MarkerStyle.Circle,
                            MarkerSize = isLastPoint ? 8 : 6
                        };
                        
                        foreach (var point in kvp.Value.Points)
                        {
                            clonedSeries.Points.AddXY(point.XValue, point.YValues[0]);
                        }
                        
                        chartResult.Series.Add(clonedSeries);
                    }
                }
                else
                {
                    // 如果没有缓存数据，则计算最终数据点
                    var finalData = allData.Take(currentIndex + 1).ToList();
                    
                    var finalFirstGrouping = finalData
                        .GroupBy(x => x)
                        .Select(g => new { Value = g.Key, Count = g.Count() })
                        .ToList();
                    
                    var finalSecondGrouping = finalFirstGrouping
                        .GroupBy(x => x.Count)
                        .Select(g => new { Count = g.Key, Frequency = g.Count() })
                        .OrderBy(x => x.Count)
                        .ToList();
                    
                    var finalSeries = new Series("最终数据")
                    {
                        ChartType = SeriesChartType.Point,
                        Color = Color.Green,
                        MarkerStyle = MarkerStyle.Circle,
                        MarkerSize = 8
                    };
                    
                    foreach (var item in finalSecondGrouping)
                    {
                        finalSeries.Points.AddXY(item.Count, item.Frequency);
                    }
                    
                    chartResult.Series.Add(finalSeries);
                }
                
                // 更新图表标题
                chartResult.Titles.Clear();
                chartResult.Titles.Add($"次数分布图 - 全部数据已计算完成");
                
                // 更新进度标签
                lblProgress.Text = $"进度: {currentIndex + 1}/{allData.Count} (已完成)";
                
                // 刷新图表
                chartResult.Invalidate();
                
                // 恢复按钮状态
                btnPrevious.Enabled = currentIndex > startPoint;
                btnNext.Enabled = currentIndex < allData.Count;
                btnRunToEnd.Enabled = currentIndex < allData.Count;
                btnCalculateOnly.Enabled = currentIndex < allData.Count;
                btnStartDraw.Enabled = true;
                
                MessageBox.Show("计算和绘图已完成！", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"计算和绘图时发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
                // 发生错误时恢复按钮状态
                btnPrevious.Enabled = currentIndex > startPoint;
                btnNext.Enabled = currentIndex < allData.Count;
                btnRunToEnd.Enabled = currentIndex < allData.Count;
                btnCalculateOnly.Enabled = currentIndex < allData.Count;
                btnStartDraw.Enabled = true;
            }
        }

        private void lblProgress_Click(object sender, EventArgs e)
        {

        }
        
        private void btnResetZoom_Click(object sender, EventArgs e)
        {
            try
            {
                // 重置X轴和Y轴的缩放
                foreach (var chartArea in chartResult.ChartAreas)
                {
                    chartArea.AxisX.ScaleView.ZoomReset();
                    chartArea.AxisY.ScaleView.ZoomReset();
                }
                
                // 刷新图表
                chartResult.Invalidate();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"重置缩放时发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}